﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zlsy.Core.ZlhisModule;

namespace Evision.Zlhis.FeeType.Personnel.Maintenance {
    public partial class 费别人员维护编辑 : BaseForm {
        private string No { get; set; }
        ApprovelControlData data = new ApprovelControlData();
        DataTable dt = new DataTable();
        public string Id { get; set; }
        public 费别人员维护编辑() {
            InitializeComponent();
        }
        private void 特殊费别审批管控编辑_Load(object sender, EventArgs e) {
            //审批费别下拉值
            string sqltype = "select 名称 from 费别 where 有效结束 > sysdate";
            dt = Adodb.QueryForTable(sqltype);
            List<string> list = new List<string>();
            foreach (DataRow row in dt.Rows) {
                // 通过列名获取费别
                string value = row["名称"].ToString();
                list.Add(value);
            }
            cmb_type.DataSource = list;
            if (!string.IsNullOrEmpty(Id)) {
                string sql = @"select 管控ID,工号,研究者姓名,费别,项目名称 from 费别人员维护 where 管控id = ?";
                DataRow dr = Adodb.Default.QueryForRow(sql, Id);
                txt_no.Text = dr["工号"].ToString();
                cmb_type.Text = dr["费别"].ToString();
                txt_name.Text = dr["研究者姓名"].ToString();
                txt_proname.Text = dr["项目名称"].ToString();
            }
        }

        private void btn_config_Click(object sender, EventArgs e) {
            try {
                if (!string.IsNullOrEmpty(Id)) {
                    data.id = Convert.ToInt64(Id);
                } else {
                    data.id = Convert.ToInt64((System.DateTime.Now - DateTime.ParseExact("20000101", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture)).TotalMilliseconds);
                }
                data.gcpcode = cmb_type.Text; //费别
                data.projectname = txt_proname.Text; //项目名称
                data.researchername = txt_name.Text; //研究者姓名
                data.jobnumber = txt_no.Text; //工号
                if (CheckValid()) {
                    Adodb.SaveOrUpdate(data);
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                    this.Close();
                } 
            } catch (Exception ex) {

                throw ex;
            }
            
        }

        private void btn_cancel_Click(object sender, EventArgs e) {
            this.Close();
        }
        private bool CheckValid() {
            if (string.IsNullOrEmpty(cmb_type.Text)) {
                MessageBox.Show("费别不能为空!");
                return false;
            } else if (string.IsNullOrEmpty(txt_proname.Text)) {
                MessageBox.Show("项目名称不能为空!");
                return false;
            } else if (string.IsNullOrEmpty(txt_name.Text)) {
                MessageBox.Show("研究者姓名不能为空!");
                return false;
            } else if (string.IsNullOrEmpty(txt_no.Text)) {
                MessageBox.Show("工号不能为空!"); 
                return false;
            } else {
                return true;
            }
           
        }


        private void txt_no_ButtonCustomClick(object sender, EventArgs e) {
            Form 特殊费别审批管控工号选择 = Application.OpenForms["特殊费别审批管控工号选择"];  //查找是否打开过窗体  
            if ((特殊费别审批管控工号选择 == null) || (特殊费别审批管控工号选择.IsDisposed)) {
                using (费别人员维护工号选择 frm = new 费别人员维护工号选择()) {
                    frm.No = txt_no.Text;
                    if (frm.ShowDialog() == DialogResult.OK) {
                        txt_no.Text = frm.No;
                        txt_name.Text = frm.DocName;
                    } else {
                        return;
                    }
                }
            } else {
                特殊费别审批管控工号选择.Close();
            }
        }

        private void txt_no_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode != Keys.Enter) {
                return;
            }
            if (string.IsNullOrEmpty(txt_no.Text.Trim())) {
                return;
            }
            Form 特殊费别审批管控工号选择 = Application.OpenForms["特殊费别审批管控工号选择"];  //查找是否打开过窗体  
            if ((特殊费别审批管控工号选择 == null) || (特殊费别审批管控工号选择.IsDisposed)) {
                using (费别人员维护工号选择 frm = new 费别人员维护工号选择()) {
                    frm.No = txt_no.Text;
                    if (frm.ShowDialog() == DialogResult.OK) {
                        MessageBox.Show(frm.No);
                        txt_no.Text = frm.No;
                        txt_name.Text = frm.DocName;
                    }
                }
            } else {
                特殊费别审批管控工号选择.Close();
            }
        }
    }
}
